﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using BDPAutodetect.Device;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// 检测任务基类
    /// </summary>
    [ObservableObject]
    public partial class DetectJobBase : PeriodicTaskAuto
    {
        /// <summary>
        /// 工位类型
        /// </summary>
        public DeviceStationType StationType { get; protected set; }
        /// <summary>
        /// 检测状态
        /// </summary>
        public DetectJobStatus DetectStatus { get; protected set; }
        /// <summary>
        /// 上一次检测状态
        /// </summary>
        public DetectJobStatus LastDetectStatus { get; protected set; }
        /// <summary>
        /// 检测结果状态
        /// </summary>
        public virtual DetectResultStatus DetectResultStatus { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argStationType">工位类型</param>
        /// <param name="argName">名称</param>
        /// <param name="argIntervalSleep">线程间隔休眠时间(单位：ms)</param>
        /// <param name="argIsStart">是否立即启动线程</param>
        /// <param name="argIsActive">是否立即激活线程</param>
        /// <param name="argIsEventWait">是否线程等待事件</param>
        /// <param name="argIsEventPause">是否线程暂停事件</param>
        protected DetectJobBase(DeviceStationType argStationType, string argName, int argIntervalSleep, bool argIsStart = false, bool argIsActive = false, bool argIsEventWait = false, bool argIsEventPause = true)
            : base(argName, argIntervalSleep, argIsStart, argIsActive, argIsEventWait, argIsEventPause)
        {
            this.StationType = argStationType;
            this.DetectStatus = DetectJobStatus.None;
            this.LastDetectStatus = DetectJobStatus.None;
            this.DetectResultStatus = DetectResultStatus.Pass;
        }

        /// <summary>
        /// 执行线程任务[继承方式必须重写]
        /// </summary>
        protected override void ExecuteTaskAction()
        {
            //激活主线程
            base.ActiveMain();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="argTData">初始化数据</param>
        public virtual void InitData<T>(T argTData)
        {
            this.DetectStatus = DetectJobStatus.None;
        }

        /// <summary>
        /// 启动检测
        /// </summary>
        public virtual async Task<bool> StartDetect()
        {
            return await Task.Run(() =>
            {
                if (!this.CheckStartPre()) return false;
                var result = this.Start();
                return result.ResultState;
            });
        }

        /// <summary>
        /// 停止检测
        /// </summary>
        public virtual async Task StopDetect()
        {
            await Task.Run(() =>
            {
                this.Pause();
            });
        }

        /// <summary>
        /// 暂停检测
        /// </summary>
        public virtual async Task PauseDetect()
        {
            await Task.Run(() =>
            {
                this.Pause();
            });
        }

        /// <summary>
        /// 继续检测
        /// </summary>
        public virtual async Task ContinueDetect()
        {
            await Task.Run(() =>
            {
                this.Active();
            });
        }

        /// <summary>
        /// 结束检测
        /// </summary>
        public virtual void FinishDetect()
        {
            this.LastDetectStatus = DetectJobStatus.None;
            this.Pause();
        }

        /// <summary>
        /// 重置清除数据
        /// </summary>
        public virtual void ResetDetect()
        {
            this.DetectStatus = DetectJobStatus.None;
            this.LastDetectStatus = DetectJobStatus.None;
        }

        /// <summary>
        /// 设置检测状态
        /// </summary>
        /// <param name="argDetectStatus">检测状态</param>
        protected virtual void SetDetectStatus(DetectJobStatus argDetectStatus)
        {
            this.DetectStatus = argDetectStatus;
        }

        /// <summary>
        /// 启动前检查
        /// </summary>
        /// <returns></returns>
        protected virtual bool CheckStartPre()
        {
            if (this.DetectStatus == DetectJobStatus.Startup) return false;
            return true;
        }
    }
}
